package com.wish.banking.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.wish.banking.entity.*;
import com.wish.banking.mapper.JrFactoringMapper;
import com.wish.banking.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wish.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 高露雯:租约保理申请表 服务实现类
 * </p>
 *
 * @author gaoluwen
 * @since 2022-04-04
 */
@Service
public class JrFactoringServiceImpl extends ServiceImpl<JrFactoringMapper, JrFactoring> implements IJrFactoringService {
    @Autowired
    IJrProductService iJrProductService;
    @Autowired
    IJrFactoringDetailService iJrFactoringDetailService;
    @Autowired
    ICfContractService iCfContractService;
    @Autowired
    ICwReceiptPlanService iCwReceiptPlanService;
    @Override
    public void saveJrFactoring(JrFactoring jrFactoring) {
        //生成 租约保理申请单号
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        sdf.format(new Date());
        jrFactoring.setFactoringOrderNo("ZYBL"+ sdf.format(new Date())+ RandomUtil.randomNumbers(4));
        //金融产品信息
        JrProduct jrProduct = iJrProductService.getById(jrFactoring.getJrProductId());
        //资方信息
        jrFactoring.setCapitalId(jrFactoring.getCapitalId());
        jrFactoring.setCapitalName(jrFactoring.getCapitalName());
        //借款金额 = 授信金额合计
        jrFactoring.setLoanAmount(jrFactoring.getFactoringCreditMoney());
        //申请状态 申请中
        jrFactoring.setApplyState(1);
        //公共信息
        Date date = new Date();
        jrFactoring.setDelFlg(false);
        jrFactoring.setCreateTime(date);
        jrFactoring.setCreateUserid(SecurityUtils.getUserId());
        jrFactoring.setUpdateTime(date);
        jrFactoring.setUpdateUserid(SecurityUtils.getUserId());
        //租约保理申请表添加数据
        baseMapper.insert(jrFactoring);
        // 传过来的对象的 租约保理明细
        List<JrFactoringDetail> jrFactoringDetailList = jrFactoring.getJrFactoringDetailList();
        if(jrFactoringDetailList!=null){
            for (JrFactoringDetail jrFactoringDetail : jrFactoringDetailList) {
                // 保理申请ID
                jrFactoringDetail.setFactoringId(jrFactoring.getId());
                // 公共信息
                jrFactoringDetail.setDelFlg(false);
                jrFactoringDetail.setCreateTime(date);
                jrFactoringDetail.setCreateUserid(SecurityUtils.getUserId());
                jrFactoringDetail.setUpdateTime(date);
                jrFactoringDetail.setUpdateUserid(SecurityUtils.getUserId());
                //向租约保理明细表中添加数据
                iJrFactoringDetailService.save(jrFactoringDetail);
                //收房合同表  冻结合同并添加金融标识
                CfContract cfContract = iCfContractService.getById(jrFactoringDetail.getCfContractId());
                //冻结  0未冻结 1已冻结
                cfContract.setFreezeStatus(1);
                //是否租约保理  是否租约保理,0:否 1:是
                cfContract.setZyblFlg(true);
                cfContract.setUpdateTime(date);
                cfContract.setUpdateUserid(SecurityUtils.getUserId());
                iCfContractService.updateById(cfContract);
                // 收款计划表 更新出房合同上未收款的收款计划收款状态:未收款->冻结状态
                CwReceiptPlan cwReceiptPlan = new CwReceiptPlan();
                cwReceiptPlan.setContractId(jrFactoringDetail.getCfContractId()); //合同id
                cwReceiptPlan.setContractType(2); //合同类型 1收房合同 2出房合同
                cwReceiptPlan.setSkStatus(4); //1:未收款 2：已收款 3：已中止  4：已冻结（未收款的时候，才会变成已冻结，所以恢复冻结状态的时候，是恢复成未收款状态）
                cwReceiptPlan.setUpdateUserid(SecurityUtils.getUserId());
                iCwReceiptPlanService.updateRemainingLeaseBill(cwReceiptPlan); //修改收款计划表
            }
        }
    }

    @Override
    public void updateFactoring(Long id, Integer applyState,String auditMemo) {
        baseMapper.updateFactoring(id,applyState,auditMemo);
    }
}
